package a;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
/**
 * @description:
 * @author: jinzewang
 * @project_name: study-springboot-demo
 * @date: 2022/9/6 10:58
 */
public class ReadTXT {
    public static void main(String[] args) throws IOException, IllegalAccessException {
        /*String path = "F:\\workspace\\onelink\\0906\\20220905\\txt\\";
        List<File> files = ReadTXT.getFiles(path);
        int count = 0;
//        System.out.println(ReadTXT.getTotalLines(path + files.get(1).getName()));
        System.out.println("文件总个数：" + files.size());
//        log.info("文件总个数：{}", files.size());
        for (File file : files) {
            int totalLines = ReadTXT.getTotalLines(path + file.getName());
            count += totalLines;
        }
        System.out.println("所有文件总行数：" + count);
//        log.info("所有文件总行数：{}", count);*/

//        String path = "F:\\workspace\\onelink\\0906\\20220905duplicate\\20220905\\txt";
        String path = "F:\\iot\\d";
        File dir = new File(path);
        List<File> allFileList = new ArrayList<>();

        int count = 0;
        // 判断文件夹是否存在
        if (!dir.exists()) {
            System.out.println("目录不存在");
            return;
        }
        getAllFile(dir, allFileList);

        List<Map<String, Integer>> mapList = new ArrayList<>();

        for (File file : allFileList) {
//            System.out.println(file.getAbsolutePath());
            String[] split = file.getName().split("_");
            String provinceName = split[1];
            HashMap<String, Integer> hashMap = new HashMap<>();
            hashMap.put(provinceName, hashMap.get(provinceName));
            int totalLines = getTotalLines(file.getAbsolutePath(), hashMap);
            count += totalLines;
        }
        System.out.println("该文件夹下共有" + allFileList.size() + "个文件");
        System.out.println("所有文件总行数：" + count);
    }

    //获取目录下文件及其子目录文件
    public static void getAllFile(File fileInput, List<File> allFileList) {
        // 获取文件列表
        File[] fileList = fileInput.listFiles();
        assert fileList != null;
        for (File file : fileList) {
            if (file.isDirectory()) {
                // 递归处理文件夹
                // 如果不想统计子文件夹则可以将下一行注释掉
                getAllFile(file, allFileList);
            } else {
                // 如果是文件则将其加入到文件数组中
                allFileList.add(file);
            }
        }
    }

    //读取所有文件
    public static List<File> getFiles(String path) {
        File root = new File(path);
        List<File> files = new ArrayList<File>();
        if (!root.isDirectory()) {
            files.add(root);
        } else {
            File[] subFiles = root.listFiles();
            for (File f : subFiles) {
                files.addAll(getFiles(f.getAbsolutePath()));
            }
        }
        return files;
    }

    // 文件内容的总行数
    static int getTotalLines(String fileName, Map<String, Integer> map) throws IOException, IllegalAccessException {
        System.out.println();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName))); //使用缓冲区的方法将数据读入到缓冲区中
        LineNumberReader reader = new LineNumberReader(br);
        String s = reader.readLine(); //定义行数
        int lines = 0;
        //产生一个随机数
        int line = 2;
        while (s != null) //确定行数
        {
            lines++;
            s = reader.readLine();
            //若随机数=lines，对该行json解析，并把lenes赋值为负数9911000744657489
            if (line == lines) {
                System.out.println("读取json的文件名：" + fileName);
                a.JsonToCount.jsonToObject(s, map);
                line = -1;
            }
        }
        reader.close();
        br.close();
        System.out.println("\n文件名：" + fileName + "\t该文件总行数：" + lines);
//        log.info("文件名：{}\t该文件总行数：{}", fileName, lines);
        return lines; //返回行数
    }
}
